// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Bootstrap the app.
 */

goog.provide('rb.App');

goog.require('goog.events.EventTarget');
goog.require('goog.structs.Map');

goog.require('rb.ServiceProvider');
goog.require('rb.auth.AuthManager');
goog.require('rb.menu.Menu');
goog.require('rb.urlMap');

goog.require('relief.cache.Cache');
goog.require('relief.nav.NavManager');
goog.require('relief.rpc.RPCService');



/**
 * The Relief Blog application.
 * @constructor
 */
rb.App = function() {
  var eventBus = new goog.events.EventTarget();

  var cache;
  /**
   * @type {relief.cache.Cache}
   * @private
   */
  this.cache_ = cache = new relief.cache.Cache();

  var headers = new goog.structs.Map({
    'Content-Type': 'application/json'
  });

  var rpc;
  /**
   * @type {relief.rpc.RPCService}
   * @private
   */
  this.rpc_ = rpc = new relief.rpc.RPCService(cache, headers);

  var auth;
  /**
   * @type {rb.auth.AuthManager}
   * @private
   */
  this.auth_ = auth = new rb.auth.AuthManager(rpc);
  auth.setParentEventTarget(eventBus);

  var iframe = /** @type {!HTMLIFrameElement} */
           (goog.dom.getElement('history_frame')),
      input = /** @type {!HTMLInputElement} */
          (goog.dom.getElement('history_input')),
      content = /** @type {!Element} */ (goog.dom.getElement('content-root'));

  var sp = new rb.ServiceProvider(eventBus, rb.urlMap, cache, rpc, auth,
                                  iframe, input, content);

  var nav;
  /**
   * @type {relief.nav.NavManager}
   * @private
   */
  this.nav_ = nav = new relief.nav.NavManager(sp);
  nav.setParentEventTarget(eventBus);

  var menu;
  /**
   * @type {rb.menu.Menu}
   * @private
   */
  this.menu_ = menu = new rb.menu.Menu(auth, nav, rpc, eventBus);
};
